package com.aya;

import com.aya.mapper.thread.SimpleThread;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.Executor;

public class SpringAnalysis {
    Logger logger = LoggerFactory.getLogger(SpringAnalysis.class);

    /**
     * 单个异步，控制台什么都不显示
     */
    @Test
    public void testSimple() {
        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        SimpleThread beansOfType = factory.getBean(SimpleThread.class);
        beansOfType.helloAsync();
    }

    @Test
    public void testSimpleException() {
        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        SimpleThread beansOfType = factory.getBean(SimpleThread.class);
        beansOfType.simpleException();
    }


    @Test
    public void testSimpleAsyncException() throws InterruptedException {
        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        SimpleThread beansOfType = factory.getBean(SimpleThread.class);
        beansOfType.asyncSimpleException("haha");
        Thread.sleep(100*1000);
    }


    @Test
    public void testSimpleNoAsync() throws InterruptedException {
        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        SimpleThread beansOfType = factory.getBean(SimpleThread.class);
        beansOfType.simple();
        Thread.sleep(100*1000);
    }



    /**
     * bean式线程池使用
     * @throws InterruptedException
     */
    @Test
    public void testService() throws InterruptedException {
        logger.info("service start");
        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        Executor executor = factory.getBean(Executor.class);
        SimpleThread simpleThread = factory.getBean(SimpleThread.class);
        for (int i = 0;i<30;i++){
            executor.execute(() -> simpleThread.simple());
        }
        for (int i = 0;i<100;i++){
            executor.execute(() -> simpleThread.simple());
            Thread.sleep(2000);
        }
        Thread.sleep(1000*1000);
    }

    /**
     * 与spring结合的线程池使用方式
     * @throws InterruptedException
     */
    @Test
    public void testQueue() throws InterruptedException {
        logger.info("queue start");
        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        SimpleThread beansOfType = factory.getBean(SimpleThread.class);
        for (int i = 0;i<30;i++){
            beansOfType.helloAsync();
        }
        Thread.sleep(100*1000);
    }


    @Test
    public void testMax() throws InterruptedException {
        logger.info("max start");
        ClassPathXmlApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        SimpleThread beansOfType = factory.getBean(SimpleThread.class);
        for (int i = 0;i<100;i++){
            beansOfType.helloAsync();
        }
       Thread.sleep(100*1000);
    }


}
